import { Injectable } from '@angular/core';
import { JPush } from '@jiguang-ionic/jpush';
import { DialogProvider } from '../../providers/dialog/dialog';
import { Device } from '@ionic-native/device';

/*
  Generated class for the JpushProvider provider.

  See https://angular.io/guide/dependency-injection for more info on providers
  and Angular DI.
*/
@Injectable()
export class JpushProvider {

  constructor(
    public jpush: JPush,
    public device: Device,
    public dialog: DialogProvider) {
    console.log('Hello JpushProvider Provider');
  }

  public initJpush() {
    this.jpush.init();
    this.jpush.setDebugMode(false);
    this.jpush.requestPermission();
    this.jpush.setApplicationIconBadgeNumber(0);
  }

  public getRegistrationID() {
    this.jpush.getRegistrationID().then((registrationID) => {

    });
  }

  //程序打开到时候接收触发
  public receiveNotification() {
    //接收通知
    document.addEventListener('jpush.receiveNotification', (event: any) => {
      this.jpush.setBadge(0);
      this.jpush.resetBadge();
      this.jpush.setApplicationIconBadgeNumber(0);
      //推送参数
      var content;
      if (this.device.platform == 'Android') {
        content = event.alert;
      } else {
        content = event.aps.alert;
      }
      //做以下处理
      this.dialog.showAlert('提示', 'Receive notification: ' + JSON.stringify(event));
    }, false);
  }

  //点击推送消息触发
  public openNotification() {
    //接收通知
    document.addEventListener('jpush.openNotification', (event: any) => {
      this.jpush.setBadge(0);
      this.jpush.resetBadge();
      this.jpush.setApplicationIconBadgeNumber(0);
      //推送参数
      var content;
      if (this.device.platform == 'Android') {
        content = event.alert;
      } else {
        content = event.aps.alert;
      }
      //做以下处理
      this.dialog.showAlert('提示', 'Receive openNotification: ' + JSON.stringify(event));
    }, false);
  }

  //加入本地推送
  addLocalNotification() {
    if (this.device.platform == 'Android') {
      this.jpush.addLocalNotification(0, 'Hello JPush', 'JPush', 1, 5000);
    } else {
      this.jpush.addLocalNotificationForIOS(5, 'Hello JPush', 1, 'localNoti1');
    }
  }
}
